const app = getApp();
import grace from "../../../utils/grace.js";
import {Address} from '../address/address-model.js';
import {Cart} from '../shopcart/cart-model.js';
import {Order} from '../order/order-model.js';

let wxpay = require('../../../utils/pay.js');

let address = new Address();
let cart = new Cart();
let order = new Order();

grace.page({

    /**
     * 页面的初始数据
     */
    data: {
        address: {},
        cart_ids: '',
        carts: [],
        productAmount: 0,// 商品总额
        deliveryPrice: 0,// 运费
        amountTotal: 0
    },
    onLoad(e) {
        this.$data.cart_ids = e.cart_ids;
        // 查询默认地址
        address.getAddressData({'default_address': true, 'token': wx.getStorageSync('token')}, (data) => {
            if (data.code == 0) {
                this.$data.address = data.data[0];
            } else {
                wx.navigateTo({
                    url: '/pages/mall/address/list?from_page=confirm'
                })
            }
        })
    },
    onShow() {
        // 查询购物车
        cart.getCartData({'cart_ids': this.$data.cart_ids, 'token': wx.getStorageSync('token')}, (data) => {
            this.$data.carts = data;

            // 计算费用
            let productAmount = 0;
            for (let i = 0; i < this.$data.carts.length; i++) {
                productAmount += this.$data.carts[i].product_id.lst_price * this.$data.carts[i].product_uom_qty;
            }
            this.$data.productAmount = productAmount;
            this.$data.amountTotal = productAmount + this.$data.deliveryPrice;
        })
    },
    paymentBtnTap() {
        let that = this;
        wx.showModal({
            title: '您确认支付吗？',
            content: '',
            success: function (res) {
                if (res.confirm) {
                    new Promise(function (resolve, reject) {
                        // 创建订单
                        order.createOrder({
                            'token': wx.getStorageSync('token'),
                            'partner_id': wx.getStorageSync('partner_id'),
                            'cart_ids': that.$data.cart_ids,
                            'delivery_id': that.$data.address.id
                        }, (data) => {
                            let order = {
                                order_id: data.order_id,
                                amount_total: data.amount_total
                            };
                            resolve(order);
                        }, (error) => {
                            reject('error');
                            console.error('error: ' + JSON.stringify(error));
                        })
                    }).then(function (order) {
                        // 支付
                        wxpay.wxpay(app, order.amount_total, order.order_id, "/pages/mall/order/detail?id=" + order.order_id);
                    })
                }
            }
        })
    },
    $onBackData(data) {
        address.getAddressData({'id': data.address_id, 'token': wx.getStorageSync('token')}, (data) => {
            if (data.code == 0) {
                this.$data.address = data.data[0];
            }
        })
    }
})